package com.example.shop_2207.detail

import android.R.attr.thumb
import android.content.Context
import android.widget.ImageView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_2207.R
import com.example.shop_2207.databinding.ActivityDetailBinding
import com.example.shop_2207.mvvm.BaseActivity
import com.example.shop_2207.net.GlideUtils
import com.google.zxing.BarcodeFormat
import com.journeyapps.barcodescanner.BarcodeEncoder
import com.umeng.socialize.ShareAction
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMWeb
import com.umeng.socialize.utils.UmengText.QQ
import com.youth.banner.loader.ImageLoader


@Route(path = "/app/DetailActivity")
class DetailActivity : BaseActivity<ActivityDetailBinding,DetailViewModel>() {
    val imglist = mutableListOf<String>()
    @Autowired
    @JvmField
    var goodsId = 0
    override fun getLayout(): Int  = R.layout.activity_detail

    override fun getViewModel(): Class<DetailViewModel>  = DetailViewModel::class.java

    override fun initView() {
        ARouter.getInstance().inject(this)
        //执行后台请求
        viewModel.detail(goodsId)
        //点击返回按钮
        binding.ivBack.setOnClickListener {
            finish()
        }
        //点击添加购物车按钮
        binding.tvAddCar.setOnClickListener {
            viewModel.addCar(1,goodsId)
        }

        binding.ivShare.setOnClickListener {
            val web: UMWeb = UMWeb("http://www.baidu.com")
            ShareAction(this)
                .withMedia(web)
                .setDisplayList(SHARE_MEDIA.QQ,SHARE_MEDIA.WEIXIN)
                .open()
        }
    }

    override fun initData() {
        viewModel.detailSuccess.observe(this){
            if(it.code==200){
                //加载轮播图
                imglist.addAll(it.data.bannerList)
                binding.banner.setImages(imglist)
                binding.banner.setImageLoader(object :ImageLoader(){
                    override fun displayImage(
                        context: Context?,
                        path: Any?,
                        imageView: ImageView?
                    ) {
                        if (context != null&&imageView != null) {
                           GlideUtils.loadImage(context, path.toString(),imageView)
                        }
                    }
                })
                binding.banner.start()

                //显示详情界面其他信息
                binding.detail = it.data
                //加详情界面中图片信息
                //GlideUtils.loadImage(this,it.data.goods_detail_one,binding.ivDetailOne)
                //GlideUtils.loadImage(this,it.data.goods_detail_two,binding.ivDetailTwo)
                //生成条形码
                val barcodeEncoder = BarcodeEncoder()
                //参数一  条形码的内容 参数二 条形码的格式  参数三  条形码的宽   参数四  条形码的高
                val bitmap = barcodeEncoder.encodeBitmap(it.data.goods_code,BarcodeFormat.CODE_128,300,100)
                binding.ercode.setImageBitmap(bitmap)


            }else{
                ToastUtils.showLong("加载详情失败：${it.message}")
            }
        }

        viewModel.detailFail.observe(this){
            ToastUtils.showLong(it)
        }

        viewModel.addSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
        viewModel.addFail.observe(this){
            ToastUtils.showLong(it)
        }

    }
}